/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package Search;

import java.util.*;
import java.io.*;

/**
 *
 * @author FY
 */
public class SubWord {


    public static ArrayList<String> getList(String prefix, int len)
    {
    	try {
			return new SubWord().getWordList(prefix, len);
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }

    public ArrayList<String> getWordList(String sub, int len) throws Exception
    {

        ArrayList<String> lst = new ArrayList<String>();
        String urlStr = "data/index.word";
        RandomAccessFile raf  = new RandomAccessFile(urlStr, "r");
        long rlen = raf.length();
//        long t1 = System.currentTimeMillis();
        long position = BinarySearch(sub, raf, 0, rlen);
        raf.seek((int)position);
        String s = null;
        int p = (int)position;
        p -= 40;
        if (p < 0) p = 0;
        raf.seek(p);
        s = raf.readLine();
        boolean b = s.startsWith(sub);
        for (int i = 1; i<= 100; i ++)
        {
            if (b == true)
            {
                break;
            }
            p = (int) raf.getFilePointer();
            raf.seek((int) p);
            s = raf.readLine();
            b = s.startsWith(sub);
        }
        for (int i = 1; i <= len; i ++)
        {

            if (s == null)
            {
                break;
            }

            lst.add(s);
            p = (int) raf.getFilePointer();
            raf.seek((int) p);
            s = raf.readLine();
        }
//        System.out.println(lst);
//        System.out.println(lst.size());
//        long t2 = System.currentTimeMillis();
//        System.out.println(t2 - t1);
        return lst;

    }
    
    public long BinarySearch(String sub, RandomAccessFile raf, long l, long r) throws IOException
    {
        String test;
    	long p = (l + r)/2;
        raf.seek((int)p);
        test = raf.readLine();
        p = (int) raf.getFilePointer();
        raf.seek((int)p);
        test = raf.readLine();
        if (r - p <= 10 || p - l <= 10)
        {
            return p;
        }
        if (sub.compareTo(test) ==  0)
        {
            return p;
        }
        else if(sub.compareTo(test) > 0)
        {
            return BinarySearch(sub, raf, p - 10, r);
        }
        else
        {

            return BinarySearch(sub, raf, 0, p + 10);
        }

    }


    public static void main (String args[]) throws Exception{

        /*
        Word w = new Word("kill");
        VerbSense s = w.getVerbSense().get(0);
        int n = s.getTopics().size();
        System.out.println(n);
        System.out.println("start:");
        System.out.println(s.getSentenceFrames());
        System.out.println(w.getTaggedCnt(POS.NOUN));
        System.out.println(w.getFamility(POS.NOUN));
        for (int i = 0; i < n; i ++)
        {
            VerbSense vs = s.getTopics().get(i);
            System.out.println(vs.words);
            System.out.println(vs.glosses);
            System.out.println("-----------------------");
        }
         *
         */
      /*  Word w = new Word("best");
            System.out.println(w.exist());
            System.out.println(w.hasRootForm());
        for (POS c: POS.values())
        {
            System.out.println(w.getRootForm( c));
        }

        VerbSense s = null;
        if (w.hasVerbSense())
        {
           s = w.getVerbSense().get(0);
            int n = s.getTroponyms().size();
            System.out.println(n);
            System.out.println("start:");
            Sense vs;
            for (int i = 0; i < n; i ++)
            {
                vs = s.getTroponyms().get(i);
                System.out.println(vs.get_Words());
                System.out.println(vs.get_Glosses());
                System.out.println("-----------------------");
            }
         }
       *
       */
    //   SubWord sw = new SubWord("0");
    //   ArrayList<String> lst = sw.getWordList("0");
       System.out.println(SubWord.getList("te", 50));
    }

}
